using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using GameFrameWork;
using UnityEngine;

/// <summary>
/// 雪花豌豆攻击策略 - 发射冰冻子弹，对目标造成伤害并减速
/// </summary>
public class SnowPeaAttackStrategy : AttackStrategyBase
{
    private SnowPeaSkillData m_SnowPeaSkillData;

    public SnowPeaAttackStrategy(SkillData data)
    {
        AttackData = data;
        if (data is SnowPeaSkillData snowPeaData)
        {
            m_SnowPeaSkillData = snowPeaData;
        }
    }

    public override void Execute(Entity target)
    {
        base.Execute(target);

        if (target == null)
        {
            Logger.LogWarning("SnowPeaAttackStrategy", $"{Owner?.name} 攻击目标无效");
            return;
        }

        if (AttackData == null || AttackData.BulletData == null)
        {
            Logger.LogError("SnowPeaAttackStrategy", $"{Owner?.name} 缺少子弹数据");
            return;
        }

        var bulletData = AttackData.BulletData;
        var bullet = BulletPool.Instance.GetBullet(bulletData);
        
        if (bullet == null)
        {
            Logger.LogError("SnowPeaAttackStrategy", $"无法获取子弹：{bulletData.BulletType}");
            return;
        }

        // 设置子弹位置
        Vector3 startPos = Owner.transform.position;
        if (bulletData.OffVec3 != Vector3.zero)
        {
            startPos += bulletData.OffVec3;
        }
        bullet.transform.position = startPos;

        // 如果是 SnowPeaBullet，设置减速参数
        if (bullet is SnowPeaBullet snowPeaBullet && m_SnowPeaSkillData != null)
        {
            snowPeaBullet.SetSlowParameters(m_SnowPeaSkillData.SlowPercentage, m_SnowPeaSkillData.SlowDuration);
        }

        // 创建移动策略
        var movement = MoveStrategyFactory.Create(bulletData.MoveData);
        bullet.Initialize(
            owner: Owner,
            movement: movement
        );

        // 播放射击动画
        AnimateShooting();
        
        Logger.Log("SnowPeaAttackStrategy", $"{Owner?.name} 发射冰冻子弹攻击 {target.name}，减速 {m_SnowPeaSkillData?.SlowPercentage * 100}%，持续 {m_SnowPeaSkillData?.SlowDuration} 秒");
    }

    private void AnimateShooting()
    {
        if (Owner == null) return;
        
        var animator = Owner.GetComponent<Animator>();
        if (animator != null)
        {
            DOTween.Sequence()
                .Append(Owner.transform.DOShakeScale(0.1f, 0.2f))
                .AppendCallback(() => animator.SetTrigger("Shoot"));
        }
    }

    public override bool CheckCondition()
    {
        return base.CheckCondition() && 
               AttackData != null && 
               AttackData.BulletData != null;
    }
}

